package com.icss.th.db;

import com.icss.th.common.config.SystemConfig;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

public class MongoConnection 
{
	private static final MongoClient client = initClient();

	private static MongoClient initClient() 
	{
		MongoClient client = null;
		
		try 
		{
			client = new MongoClient(new MongoClientURI(SystemConfig.MONGODB_ADDRESS));
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return client;
	}
	
	private static DB getDB()
	{
		return client == null? null : client.getDB(SystemConfig.MONGODB_DBNAME);
	}
	
	public static DBCollection getCollection(String name)
	{
		DB db = getDB();
		db.authenticate(SystemConfig.MONGODB_USER, SystemConfig.MONGODB_PWD.toCharArray());
		return db == null? null : db.getCollection(name);
	}
}
